package com.wit.health.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;



/**
*   从设备数据字符串中分别提取九轴数据的工具类
*
* @Author liaozhaoping
* @Date 2024/5/21 16:38
*/
public class DataExtractor {

    
    /**  
    *  从设备数据字符串中提取加速度XYZ的三轴数据  
    *  
    * @Author liaozhaoping
    * @Date 2024/5/21 16:38  
    */
    public static float[] extractAcceleration(String data) {
        // 正则表达式匹配加速度数据
        Pattern pattern = Pattern.compile("加速度X:(-?\\d+\\.\\d+)g\\s+加速度Y:(-?\\d+\\.\\d+)g\\s+加速度Z:(-?\\d+\\.\\d+)g");
        Matcher matcher = pattern.matcher(data);

        // 准备一个数组来存储加速度X, Y, Z的值
        float[] accelerations = new float[3];

        if (matcher.find()) {
            // 将匹配到的字符串转换为float并存入数组
            accelerations[0] = Float.parseFloat(matcher.group(1));
            accelerations[1] = Float.parseFloat(matcher.group(2));
            accelerations[2] = Float.parseFloat(matcher.group(3));
        }

        return accelerations;
    }


    /**
    * 从设备数据字符串中提取角度XYZ的三轴数据
    *
    * @Author liaozhaoping
    * @Date 2024/5/22 16:03
    */
    public static float[] extractAngles(String dataString) {
        float[] angles = new float[3]; // 初始化数组来存储X, Y, Z角度

        // 定义正则表达式来匹配角度行
        String regex = "角度X:(-?\\d+\\.?\\d*)°\\s+角度Y:(-?\\d+\\.?\\d*)°\\s+角度Z:(-?\\d+\\.?\\d*)°";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(dataString);

        // 检查是否找到匹配
        if (matcher.find()) {
            // 提取并转换角度
            angles[0] = Float.parseFloat(matcher.group(1));
            angles[1] = Float.parseFloat(matcher.group(2));
            angles[2] = Float.parseFloat(matcher.group(3));
        }
        return angles;
    }

    /**
    *  从设备数据字符串中提取磁场XYZ的三轴数据
    *
    * @Author liaozhaoping
    * @Date 2024/5/22 16:26
    */
    public static float[] extractMagneticFields(String dataString) {
        float[] magneticFields = new float[3]; // Initialize array to store the magnetic field values

        // Define the regular expression to match the line containing magnetic field data
        String regex = "磁场X:(-?\\d+\\.?\\d*)μT\\s+磁场Y:(-?\\d+\\.?\\d*)μT\\s+磁场Z:(-?\\d+\\.?\\d*)μT";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(dataString);

        // Check if there is a match
        if (matcher.find()) {
            // Extract and convert magnetic field values
            magneticFields[0] = Float.parseFloat(matcher.group(1));
            magneticFields[1] = Float.parseFloat(matcher.group(2));
            magneticFields[2] = Float.parseFloat(matcher.group(3));
        }

        return magneticFields;
    }


}
